import { createStore } from 'vuex'
import { sessionStorage } from '@/utils/index'
import playList from './playList/index.js'
import * as axiosApi from '@/views/login/api'

export default createStore({
  state: {
    loading: false, //http请求等待标志
    userInfor: sessionStorage('userInfor') || { vipGrade: {} }
  },
  mutations: {
    /**
     * @description: 全局loading
     */
    STARTLOADING(state) {
      state.loading = true
    },
    ENDLOADING(state) {
      state.loading = false
    },
    /**
     * @description: 设置userInfor
     */
    SETUSERINFOR(state, value) {
      state.userInfor = value
      sessionStorage('userInfor', value)
    },
    /**
     * @description: 清除userInfor
     */
    CLEARUSERINFOR(state) {
      state.userInfor = { vipGrade: {} }
      window.sessionStorage.removeItem('userInfor')
    }
  },
  actions: {
    updateInfor({ commit }) {
      axiosApi.getSession().then(res => {
        // 对地区选择框做处理
        res.data.city = res.data.city.join('/')
        commit('SETUSERINFOR', res.data)
      })
    },
    addGrade({commit,state}){
      axiosApi.addExperience().then(res=>{
        state.userInfor.vipGrade=res.data
        commit('SETUSERINFOR', state.userInfor)
      })
    }
  },
  modules: {
    playList
  }
})
